package com.lishe.lishe.lishe.module.auth.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.animation.LinearOutSlowInInterpolator;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.lishe.lishe.R;
import com.lishe.lishe.lishe.module.auth.adapter.FragmentViewPagerAdapter;
import com.lishe.lishe.lishe.module.auth.fragment.ActiveEndFragment;
import com.lishe.lishe.lishe.module.auth.fragment.ActiveFragment;
import com.lishe.lishe.lishe.module.auth.fragment.AuthBaseFragment;
import com.lishe.lishe.lishe.module.auth.fragment.ForgetPwdEndFragment;
import com.lishe.lishe.lishe.module.auth.fragment.ForgetPwdFragment;
import com.lishe.lishe.lishe.module.auth.fragment.LoginFragment;
import com.lishe.lishe.lishe.module.auth.fragment.RegEndFragment;
import com.lishe.lishe.lishe.module.auth.fragment.RegFragment;
import com.lishe.lishe.lishe.module.auth.fragment.UserProtocolFragment;
import com.lishe.lishe.lishe.module.auth.view.FixedSpeedScroller;
import com.lishe.lishe.lishe.module.auth.view.ViewPagerUntouchable;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhcase on 2017/9/5.
 * 登陆注册修改找回密码的住Activity
 */

public class AuthActivity extends FragmentActivity implements AuthBaseFragment.FragmentJumper {

    private static final String TAG = AuthActivity.class.getSimpleName();
    private ViewPagerUntouchable mViewPager;

    //private List<AuthBaseFragment> initFragments = new ArrayList<AuthBaseFragment>();
    private List<AuthBaseFragment> fragments = new ArrayList<AuthBaseFragment>();

    private FragmentViewPagerAdapter mPagerAdapter = null;

    public static void jump(Activity activity){
        Intent intent=new Intent(activity,AuthActivity.class);
        activity.startActivity(intent);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_auth);

        mViewPager = findViewById(R.id.viewPager);
        mViewPager.setScrollable(false);
        //mViewPager.setOffscreenPageLimit(1);

        /**
         * 通过反射来修改 ViewPager的mScroller属性
         */
        try {
            Class clazz = Class.forName("android.support.v4.view.ViewPager");
            Field f = clazz.getDeclaredField("mScroller");
            FixedSpeedScroller fixedSpeedScroller = new FixedSpeedScroller(this, new LinearOutSlowInInterpolator());
            fixedSpeedScroller.setmDuration(500);
            f.setAccessible(true);
            f.set(mViewPager, fixedSpeedScroller);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //qyx修改
        fragments.clear();
        fragments.add(LoginFragment.newInstance().setJumper(this));//0
        fragments.add(RegFragment.newInstance().setJumper(this));//1
        fragments.add(RegEndFragment.newInstance().setJumper(this));//2
        fragments.add(ActiveFragment.newInstance().setJumper(this));//3
        fragments.add(ActiveEndFragment.newInstance().setJumper(this));//4
        fragments.add(ForgetPwdFragment.newInstance().setJumper(this));//5
        fragments.add(ForgetPwdEndFragment.newInstance().setJumper(this));//6
        fragments.add(UserProtocolFragment.newInstance().setJumper(this));//7


        //mPagerAdapter = new FragmentViewPagerAdapter(this,this.getSupportFragmentManager(), mViewPager, fragments,fragments.get(0));
        mPagerAdapter = new FragmentViewPagerAdapter(this, this.getSupportFragmentManager(), fragments, mViewPager);

        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setOnPageChangeListener(mPagerAdapter);


        jumpToFragmentForward(0);

    }


    @Override
    protected void onResume() {
        super.onResume();
        mPagerAdapter.notifyDataSetChanged();


    }

    @Override
    protected void onPause() {

        //showSystemUI();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

    public boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    @Override
    public void jumpToFragmentForward(final int fragmentIndex) {

        if (mPagerAdapter != null) {
            mPagerAdapter.gotoFragmentForward(fragmentIndex);
        }
    }

    @Override
    public void jumpToFragmentBack(final int fragmentIndex) {

        if (mPagerAdapter != null) {
            mPagerAdapter.gotoFragmentBack(fragmentIndex);
//            runOnUiThread(new Runnable() {
//                @Override
//                public void run() {
//                    mViewPager.setCurrentItem(fragmentIndex, true);
//                }
//            });

        }
    }

    @SuppressLint("NewApi")
    public void hideSystemUI() {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
    }

    @SuppressLint("NewApi")
    public void showSystemUI() {
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }

}

